<?php
/**
 * Created by PhpStorm.
 * User: jinji
 * Date: 2017/10/30
 * Time: 15:20
 */

namespace App\Services;


use App\Model\Comment;

class CommentService
{
    /**得到所有评论的子孙评论
     * @param $post_id
     * @return array
     */
    public function getComments($post_id)
    {
        $all = Comment::where('post_id',$post_id)->get();
        $forest = array();
        $forest = $this->getTree($all,Comment::ROOT_COMMENT);
        return $forest;
    }

    /**
     * 构造评论树
     * @param $data
     * @param $pId
     * @return array
     */
    private function getTree($data, $pId)
    {
        $tree = [];
        foreach ($data as $k => $v) {
            if ($v['parent'] == $pId) {
                $v['children'] = $this->getTree($data, $v['id']);
                $tree[] = $v;
            }
        }
        return $tree;
    }
}